package com.hertz.alarm.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.hertz.alarm.util.Constants4HTTP;
import com.hertz.alarm.util.RequestUtil;

public class LoginInterceptor extends HandlerInterceptorAdapter {

	public boolean preHandle(HttpServletRequest request, 
			HttpServletResponse response, Object handler)
		    throws Exception {
			//boolean login = false;
			String uname = RequestUtil.getCookieValue(request, Constants4HTTP.SYS_UNAME);
			String token = RequestUtil.getCookieValue(request, Constants4HTTP.SYS_TOKEN);
			if(StringUtils.isBlank(uname) || StringUtils.isBlank(token)){
				response.sendRedirect("/login");
				return false;
			}
			if(token.equals(RequestUtil.generateToken(uname, Constants4HTTP.serial.toString()))){
				return true;
			}else{
				response.sendRedirect("/login");
				return false;
			}
		}
}
